﻿using System;
using System.IO;
using System.Threading;

namespace price.ro
{
    class Program
    {
        static void Main(string[] args)
        {
            HTMLGrabSiProxyGen GrabProx = new HTMLGrabSiProxyGen();
            HTMLGrabSiProxyGen.GenerateProxy();

            #region Parseaza o categorie de produse (carti)
            for (int i = 1; i < 1520; i++)//1520 de pagini cu produse (carti)
            {
                string pagini;
                bool Reparse = false;
            Inceput:
                pagini = HTMLGrabSiProxyGen.Grab("http://www.price.ro/index.php?action=produse&categ_id=1677&page=" + i.ToString(), true);
                File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "sites.txt", "http://www.price.ro/index.php?action=produse&categ_id=1677&page=" + i.ToString() + " " + DateTime.Now.ToString("HH:mm:ss") + "\r\n");

                int index = 0;
                for (int j = 0; j < 20; j++)//20 de produse/pargina
                {
                    Console.WriteLine("Main - for 0-20: i-" + i.ToString() + " j-" + j.ToString() + " " + DateTime.Now.ToString("HH:mm:ss"));//--
                    try
                    {
                        #region Parsez price.ro
                        string link1 = String.Empty, link2 = String.Empty, link3 = String.Empty;
                        try
                        {
                            index = pagini.IndexOf("<tr class=trList", index + 10);
                            if (index == -1) { HTMLGrabSiProxyGen.ProxyCuInterfata(); goto Inceput; }//ma aflu pe cazul proxy-ului cu interfata

                            int index2 = pagini.IndexOf("<td class=\"text_11\" valign=\"top\"><a href=\"", index);
                            string sublink = pagini.Substring(index2 + 1 + "<td class=\"text_11\" valign=\"top\"><a href=\"".Length, pagini.IndexOf("\"", index2 + 1 + "<td class=\"text_11\" valign=\"top\"><a href=\"".Length) - (index2 + 1 + "<td class=\"text_11\" valign=\"top\"><a href=\"".Length));
                            sublink = sublink.Substring(7);
                            link1 = "http://www.price.ro/preturi" + sublink;
                            link2 = "http://www.price.ro/specificatii" + sublink;
                            link3 = "http://www.price.ro/review" + sublink;
                        }
                        catch (Exception e) { if (!Reparse) { Reparse = true; goto Inceput; } else File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "logs.txt", e.Message + "\t not done:" + link1 + "\t" + DateTime.Now.ToString("HH:mm:ss") + "\r\n"); }
                        #endregion

                        #region Start Threading
                        AutoResetEvent autoEvent = new AutoResetEvent(false);
                        AutoResetEvent autoEvent2 = new AutoResetEvent(false);
                        try
                        {
                            HTMLGrabSiProxyGen.WaitSemaphore();
                            ThreadPool.QueueUserWorkItem(new WaitCallback(IaProdusPreturi), new ProdusTh() { it = new item() { link1 = link1 }, autoEvent = autoEvent });
                        }
                        catch (Exception ex) { File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "logs.txt", ex.Message + "\r\n"); }
                        finally
                        { HTMLGrabSiProxyGen.ReleaseSemaphore(); }
                        try
                        {
                            HTMLGrabSiProxyGen.WaitSemaphore();
                            ThreadPool.QueueUserWorkItem(new WaitCallback(IaProdusSpecificatii), new ProdusTh() { it = new item() { link2 = link2 }, autoEvent = autoEvent2 });
                        }
                        catch (Exception ex) { File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "logs.txt", ex.Message + "\r\n"); }
                        finally
                        { HTMLGrabSiProxyGen.ReleaseSemaphore(); }

                        autoEvent.WaitOne(HTMLGrabSiProxyGen.GetSecundeTimeout());
                        autoEvent2.WaitOne(HTMLGrabSiProxyGen.GetSecundeTimeout());
                        #endregion
                    }
                    catch (Exception ex)
                    { File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "logs.txt", ex.Message + "\r\n"); }
                }
            }
            #endregion
        }

        #region Parse un produs
        private static void IaProdusPreturi(object state)
        {
            item i = ((ProdusTh)state).it;

            string itemMagazine = HTMLGrabSiProxyGen.Grab(i.link1, true);
            File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "subSites.txt", i.link1 + " " + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
            ((ProdusTh)state).autoEvent.Set();
        }
        private static void IaProdusSpecificatii(object state)
        {
            item i = ((ProdusTh)state).it;

            string itemSpecs = HTMLGrabSiProxyGen.Grab(i.link2, true);
            File.AppendAllText(HTMLGrabSiProxyGen.GetDirector() + "subSites.txt", i.link2 + " " + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
            ((ProdusTh)state).autoEvent.Set();
        }
        #endregion



    }
}